import { execute } from '../src/execute-step';
import * as upload from '../src/package-upload';
import * as OSS from '../src/oss-upload';
import * as sinon from 'sinon';
import { env } from 'process';
import mock from 'mock-fs'
import * as util from '../src/util'
import mdFile from 'md5-file'
import * as uploadToPackageFile from '../src/package-file-upload'


describe('execute', () => {
    let artifactUploadStub: sinon.SinonStub;
    let uploadMetaDataStub: sinon.SinonStub;
    let uploadToPackageFileStorageStub: sinon.SinonStub;

    let OSSPutStub: sinon.SinonStub;
    let utilStub: sinon.SinonStub;
    let mdFileStub: sinon.SinonStub;

    beforeEach(() => {
        mock({
            '/sss1/packages': {
                'source': 'file content'
            },
            '/sss1/packages/sss7': {
                'source': 'file content',
                'sss7':'file content',
            },

            '/sss4/packages': {
                'source': 'file content',
            },
        });
        env['WORK_SPACE']='/sss1'
        env['PROJECT_DIR']='/sss2'
        env['uploadType']='sss3'
        env['ARCHIVE_PATH']='/sss4'
        env['ARTIFACT']='sss5'
        env['ARTIFACT']='sss6'
        env['ARTIFACT_NAME']='sss7'
        env['DATETIME']='sss8'
        env['stepIdentifier']='sss9'
        env['FILE_PARENT_ID']='sss0'
        env['ENGINE_GLOBAL_PARAM_ORGANIZATION_ID']='sss11'
        artifactUploadStub = sinon.stub(upload, 'artifactUpload');
        uploadMetaDataStub = sinon.stub(upload, 'uploadMetaData');
        OSSPutStub = sinon.stub(OSS, 'put');
        utilStub = sinon.stub(util, 'tarPack');
        mdFileStub = sinon.stub(mdFile, 'sync');
        uploadToPackageFileStorageStub =  sinon.stub(uploadToPackageFile,'uploadToPackageFileStorage');
    });

    afterEach(() => {
        artifactUploadStub.restore();
        uploadMetaDataStub.restore();
        OSSPutStub.restore();
        utilStub.restore();
        mdFileStub.restore();
        mock.restore();
        uploadToPackageFileStorageStub.restore();
    });

    it('should call artifactUpload and uploadMetaData when uploadType is PACKAGES', async () => {
        env['uploadType']='PACKAGES'

        await execute();
        sinon.assert.calledOnce(uploadMetaDataStub);


    });

    it('should call uploadToPackageFileStorage when uploadType is PACKAGE_FILE_STORAGE', async () => {
        env['uploadType']='PACKAGE_FILE_STORAGE'

        await execute();
        sinon.assert.calledOnce(uploadToPackageFileStorageStub);

    });

    it('should call OSS.put when uploadType is OSS', async () => {

        await execute();

        sinon.assert.calledOnce(OSSPutStub);
    });
});
